package com.ruoyi.system.xmlAbstract.context;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.system.xmlAbstract.XmlGenerateAbstract;
import com.ruoyi.system.xmlAbstract.dto.FrontProcessorData;
import com.ruoyi.system.xmlAbstract.interfaces.analysisInterfaces.XmlTemplateAnalysis;
import com.ruoyi.system.xmlAbstract.interfaces.analysisInterfaces.impl.XmlAnalysis;
import com.ruoyi.system.xmlAbstract.interfaces.fileGenerate.FileGenerateInterface;
import com.ruoyi.system.xmlAbstract.interfaces.fileGenerate.impl.XmlFileGenerate;
import com.ruoyi.system.xmlAbstract.interfaces.paramInterfaces.XmlAutoFillParamInterface;
import com.ruoyi.system.xmlAbstract.interfaces.paramInterfaces.impl.XmlFillParamFromMap;
import com.ruoyi.system.xmlAbstract.interfaces.templateFactory.TemplateFactory;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : anxinr
 * @date : 2023/1/16 17:46
 */
@Slf4j
@Setter
@Getter
public class XmlBuildContext {
    private XmlBuildContext(){
    }
    private static XmlBuildContext context = null ;

    /**解析模板对象*/
    private XmlTemplateAnalysis analysis ;
    /**操作的 XML 类*/
    private XmlGenerateAbstract xmlBuild ;
    /**生成 XML 文件 类*/
    private FileGenerateInterface file ;
    /**填充 XML 数据 类*/
    private XmlAutoFillParamInterface autoFillParam ;

    /**
     * 自定义初始化数据
     * @param templateCode
     * @param analysis
     * @param file
     * @throws Exception
     */
    public void initXmlData(String templateCode,XmlTemplateAnalysis analysis,FileGenerateInterface file,XmlAutoFillParamInterface autoFillParam) throws Exception {
        this.xmlBuild = this.generateXmlTemplate(templateCode);
        this.analysis = analysis ;
        this.file = file ;
        this.autoFillParam = autoFillParam ;
    }

    /**
     * 使用默认初始化数据
     * @param templateCode
     * @throws Exception
     */
    public void initXmlData(String templateCode) throws Exception {
        this.xmlBuild = this.generateXmlTemplate(templateCode);
        this.analysis = new XmlAnalysis();
        this.file = new XmlFileGenerate();
        this.autoFillParam = new XmlFillParamFromMap() ;
    }

    /**
     * XML 类解析方法
     * @throws Exception
     */
    public void analysisXmlTemplate() throws Exception {
        if (ObjectUtil.isEmpty(this.analysis)){
            throw new Exception("没有设置解析模板对象") ;
        }
        this.analysis.analysisXmlTemplate(this.xmlBuild);
    }

    /**
     * 生成文件
     * @param path
     * @throws Exception
     */
    public void generateFile(String path,String fileName,String data) throws Exception{
        file.generateFile(path,fileName,data);
    }

    /**
     * 填充参数
     * @param info
     * @throws Exception
     */
    public void fillParamInfo(Object info){
        this.autoFillParam.fillParam(this.xmlBuild,info) ;
    }

    /**
     * 获取xml模板对象
     * @param code
     * @return
     * @throws Exception
     */
    private XmlGenerateAbstract generateXmlTemplate(String code) throws Exception {
        return TemplateFactory.getInstance(code);
    }

    /**
     * 解析前置机返回文件中的信息
     * @param fileName
     * @return
     * @throws Exception
     */
    public static List<FrontProcessorData> analysisFileInfoByFileName(String fileName) throws Exception {
        /**返回读取到的数据*/
        List<FrontProcessorData> list = new ArrayList<>() ;
        /**读取文件信息*/
        List<String> dataList = FileUtil.readLines(fileName, "GBK");
        if (CollectionUtil.isNotEmpty(dataList)){
            for (String data : dataList) {
                log.info("读到的数据为 -> {}",data);
                if (StringUtils.isNotBlank(data)){
                    /**处理数据*/
                    FrontProcessorData frontProcessorData = new FrontProcessorData();
                    /**反射获取所有字段信息*/
                    Field[] declaredFields = frontProcessorData.getClass().getDeclaredFields();
                    /**解析数据*/
                    String[] split = data.split("\\|");
                    for (int i = 0; i < split.length; i++) {
                        Field declaredField = declaredFields[i];
                        /**开启权限*/
                        declaredField.setAccessible(true);
                        /**写入数据到对象中*/
                        declaredField.set(frontProcessorData,split[i]);
                    }
                    list.add(frontProcessorData) ;
                }
            }
        }
        return list ;
    }

    /**
     * 获取对象
     * @return context实例
     */
    public static XmlBuildContext getInstance(){
        context = new XmlBuildContext() ;
        return  context;
    }

    /**
     * 获取 BatchFileName
     * @param data
     * @return
     */
    public static String getBatchFileName(String data){
        String[] batchFileNames = data.split("BatchFileName");
        String batchFileName = batchFileNames[1];
        String replace = batchFileName.replace(">", "").replace("</", "");
        return replace ;
    }
}
